<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>内容类别管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/layuimini/lib/layui-v2.5.5/css/layui.css}"
          href="../../../static/layuimini/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" th:href="@{/layuimini/css/public.css}" href="../../../static/layuimini/css/public.css"
          media="all">
    <style>
        body {
            background-color: #fff;
            padding: 0;
            margin: 0;
        }

        body .layui-layer-btn {
            background-color: #F8F8F8;
            border-top: 1px solid #eee;
            padding-top: 10px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <form class="layui-form" lay-filter="tableToolForm">
            <div class="layui-form-item">
                <div class="layui-inline">
                    类别搜索
                </div>
                <div class="layui-inline">
                    <input id="typeName" class="layui-input" name="typeName" type="text" placeholder="输入关键字">
                </div>
                <input type="hidden" name="type" value="1">
                <div class="layui-inline">
                    <button class="layui-btn" id="btn-search">
                        <i class="layui-icon">&#xe615;</i>搜索
                    </button>
                    <button class="layui-btn layui-btn-primary" id="btn-reload">
                        <i class="layui-icon">&#xe666;</i>清空搜索
                    </button>
                    <button class="layui-btn layui-btn-normal" id="btn-add">
                        <i class="layui-icon">&#xe624;</i>新增
                    </button>
                    <button class="layui-btn layui-btn-danger" id="btn-delete">
                        <i class="layui-icon">&#xe640;</i>删除
                    </button>
                </div>
            </div>
        </form>

        <table id="contentTypeTable"></table>


    </div>
</div>
<script th:src="@{/layuimini/lib/layui-v2.5.5/layui.js}" src="../../../static/layuimini/lib/layui-v2.5.5/layui.js"
        charset="utf-8"></script>
<script th:src="@{/layuimini/js/lay-config.js?v=1.0.4}" src="../../../static/layuimini/js/lay-config.js?v=1.0.4"
        charset="utf-8"></script>
<script>
    layui.use(['form', 'treeTable'], function () {
        var $ = layui.jquery,
            form = layui.form,
            treeTable = layui.treeTable;

        var insTb = treeTable.render({
            elem: '#contentTypeTable',
            url: '[[@{/}]]manage/content-type/list',
            tree: {
                iconIndex: 2,
                isPidData: true,
                idName: 'typeId',
                pidName: 'parentId',
            },
            defaultToolbar: ['filter', 'print', 'exports', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [
                    {type: 'checkbox', minWidth: 50},
                    {field: 'typeId', title: 'ID', minWidth: 50, width: 50},
                    {field: 'typeName', title: '类别名称', minWidth: 120},
                    {field: 'title', title: 'seo标题', minWidth: 120},
                    {field: 'description', title: '描述', minWidth: 120},
                    {field: 'keywords', title: '关键词', minWidth: 120},
                    {field: 'orderNum', title: '排序', minWidth: 120},
                    {
                        title: '操作', minWidth: 210,width:210, align: "right", templet: function (d) {
                            var btn = '';
                            if (d.parentId==null||d.parentId==-1){
                                btn='<button class="layui-btn layui-btn-normal layui-btn-xs  layui-btn-radius" lay-event="add"><i class="layui-icon">&#xe642;</i>新增</button>';
                            }
                            btn +=
                                '<button class="layui-btn layui-btn-xs  layui-btn-radius" lay-event="edit"><i class="layui-icon">&#xe642;</i>编辑</button>' +
                                '<button class="layui-btn layui-btn-xs layui-btn-danger layui-btn-radius" lay-event="delete"><i class="layui-icon">&#xe640;</i>删除</button>';

                            return btn;
                        }
                    }

                ]
            ],
            style: 'margin-top:0;'
        });

        form.on('submit(tableToolForm)', function (data) {
            return false;
        });

        //搜索
        $('#btn-search').click(function () {
            var typeName = $('#typeName').val();

            if (typeName.trim().length == 0) {
                insTb.clearFilter();
            }
            insTb.filterData(typeName);
        });
        //清空搜索
        $('#btn-reload').click(function () {
            $('#typeName').val('');
            insTb.clearFilter();
        });
        //新增
        $('#btn-add').click(function () {
            layer.open({
                type: 2
                , title: '新增内容类别'
                , content: '[[@{/}]]manage/content-type/content-type-edit.html'
                , resize: false
                , area: ['500px', '440px']
                , btn: ['保存', '取消']
                , yes: function (index) {
                    var iframeWindow = window['layui-layer-iframe'+ index];
                    var editWindow = $(iframeWindow.document);
                    if (editWindow.find('#typeName').val().trim().length<1){
                        layer.msg('类别名不能为空！');
                        return ;
                    }
                    var postData = editWindow.find('#contentTypeForm').serialize();
                    layer.load(2);
                    $.ajax({
                        type: 'post',
                        url: '[[@{/}]]manage/content-type/typeSave',
                        data: postData,
                        dataType: 'json',
                        success: function (result) {
                            console.log(result);
                            if (result.success) {
                                layer.msg("操作成功");
                                editWindow.find('#typeId').val(result.data.typeId);
                            } else {
                                layer.msg(result.msg);
                            }
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            layer.msg(textStatus);
                        },
                        complete: function () {
                            layer.closeAll('loading');
                        }
                    });
                }
                , end: function () {
                    insTb.reload();
                }
            });
        });



        //批量删除
        $('#btn-delete').click(function () {
            var dataList = insTb.checkStatus(false);
            var ids = [];
            $(dataList).each(function () {
                ids.push(this.typeId);
            });
            console.log(ids);
            layer.confirm('真的删除么', function (index) {
                $.ajax({
                    type: 'get',
                    url: '[[@{/}]]manage/content-type/delete',
                    data: {contentTypeIds: ids},
                    traditional: true,//传统方式传递数组
                    dataType: 'json',
                    success: function (result) {
                        console.log(result);
                        if (result.success) {
                            layer.msg("操作成功", {shift: -1, time: 500}, function () {
                                insTb.reload();
                            });

                        } else {
                            layer.msg(result.msg);
                        }
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        layer.msg(textStatus);
                    },
                    complete: function () {
                        layer.closeAll('loading');
                    }
                });
            });

        });


        treeTable.on('tool(contentTypeTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                layer.open({
                    type: 2
                    , title: '修改类别 [id:' + data.typeId + ']'
                    , content: '[[@{/}]]manage/content-type/content-type-edit.html?typeId=' + data.typeId
                    , resize: false
                    , area: ['500px', '440px']
                    , btn: ['保存', '取消']
                    , yes: function (index) {
                        var iframeWindow = window['layui-layer-iframe'+ index];
                        var editWindow = $(iframeWindow.document);
                        if (editWindow.find('#typeName').val().trim().length<1){
                            layer.msg('类别名不能为空！');
                            return ;
                        }
                        var postData = editWindow.find('#contentTypeForm').serialize();
                        layer.load(2);
                        $.ajax({
                            type: 'post',
                            url: '[[@{/}]]manage/content-type/typeSave',
                            data: postData,
                            dataType: 'json',
                            success: function (result) {
                                console.log(result);
                                if (result.success) {
                                    layer.msg("操作成功");
                                } else {
                                    layer.msg(result.msg);
                                }
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                layer.msg(textStatus);
                            },
                            complete: function () {
                                layer.closeAll('loading');
                            }
                        });
                    }
                    , end: function () {
                        insTb.reload();
                    }
                });
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除么', function (index) {
                    layer.load(2);
                    $.ajax({
                        type: 'get',
                        url: '[[@{/}]]manage/content-type/delete',
                        data: {contentTypeId: data.typeId},
                        dataType: 'json',
                        success: function (result) {
                            console.log(result);
                            if (result.success) {
                                layer.msg("操作成功");
                                insTb.reload();
                            } else {
                                layer.msg(result.msg);
                            }
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            layer.msg(textStatus);
                        },
                        complete: function () {
                            layer.closeAll('loading');
                        }
                    });

                    layer.close(index);
                });
            } else if (obj.event === 'add') {
                layer.open({
                    type: 2
                    ,
                    title: '新增内容类别'
                    ,
                    content: '[[@{/}]]manage/content-type/content-type-edit.html?parentId=' + data.typeId + '&parentTypeName=' + data.typeName
                    ,
                    resize: false
                    ,
                    area: ['500px', '440px']
                    ,
                    btn: ['保存', '取消']
                    ,
                    yes: function (index,layero) {
                        var iframeWindow = window['layui-layer-iframe'+ index];
                        var editWindow = $(iframeWindow.document);
                        if (editWindow.find('#typeName').val().trim().length<1){
                            layer.msg('类别名不能为空！');
                            return ;
                        }
                        var postData = editWindow.find('#contentTypeForm').serialize();
                        layer.load(2);
                        $.ajax({
                            type: 'post',
                            url: '[[@{/}]]manage/content-type/typeSave',
                            data: postData,
                            dataType: 'json',
                            success: function (result) {
                                console.log(result);
                                if (result.success) {
                                    layer.msg("操作成功");
                                    editWindow.find('#typeId').val(result.data.typeId);
                                } else {
                                    layer.msg(result.msg);
                                }
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                layer.msg(textStatus);
                            },
                            complete: function () {
                                layer.closeAll('loading');
                            }
                        });
                    }
                    ,
                    end: function () {
                        insTb.reload();
                    }
                });
            }
        });

    });
</script>

</body>
</html>